﻿Imports System.Data.SqlClient
Imports System.Data

Partial Class private_secure_members_manage_MyCardDetails
    Inherits System.Web.UI.Page
    Dim conn As SqlConnection
    Dim com As SqlCommand
    Dim reader As SqlDataReader
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
    Dim carddetailsTA As New LogginsLogDSTableAdapters.usp_CardDetailsSelectTableAdapter()
    Dim carddetailsDT As LogginsLogDS.usp_CardDetailsSelectDataTable

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load


        'If Session("SelectedEntityID") Is Nothing Then
        '    Response.Redirect("~/private/secure_members/payment/BuyCredit.aspx")

        'End If


        carddetailsDT = carddetailsTA.GetData(Nothing)

        Dim cards = From q In carddetailsDT.AsEnumerable() Where q.EntityId = CDbl(Session("SelectedEntityID").ToString()) Select q
        GridView1.DataSource = cards
        GridView1.DataBind()

        lblEditDeleteTitle.Text = "Credit Card details"
        If Not Page.IsPostBack Then
            'pnlEditCard.Visible = False
            'lblEditDeleteTitle.Text = "Select Credit Card for edit"

            FillYearsExpiry()
            FillYearsValidy()
        End If
    End Sub

    Sub UpdateCreditCard(Id As Integer, _
                     EntityId As Integer, _
                     FirstName As String, _
                     LastName As String, _
                     CardNumber As String, _
                     CVV As String, _
                     L4D As String, _
                     CardTypeId As Integer, _
                     ExpireMonth As Integer, _
                     ExpireYear As Integer, _
                     ValidFromMonth As Integer, _
                     ValidFromYear As Integer, _
                     LastBilled As DateTime, _
                     Active As Boolean
                     )
        conn = New SqlConnection(connectionString)
        com = New SqlCommand()

        com.Connection = conn

        com.CommandType = CommandType.StoredProcedure

        com.CommandText = "pay.usp_CardDetailsUpdate"

        com.Parameters.Add("@Id", Data.SqlDbType.Int).Value = Id
        com.Parameters.Add("@EntityId", Data.SqlDbType.Int).Value = EntityId
        com.Parameters.Add("@FirstName", Data.SqlDbType.Text).Value = FirstName
        com.Parameters.Add("@LastName", Data.SqlDbType.Text).Value = LastName
        com.Parameters.Add("@CardNumber", Data.SqlDbType.Text).Value = CardNumber
        com.Parameters.Add("@L4D", Data.SqlDbType.Text).Value = L4D
        com.Parameters.Add("@CVV", Data.SqlDbType.Text).Value = CVV
        com.Parameters.Add("@CardTypeId", Data.SqlDbType.Int).Value = CardTypeId
        com.Parameters.Add("@ExpireMonth", Data.SqlDbType.Int).Value = ExpireMonth
        com.Parameters.Add("@ValidFromMonth", Data.SqlDbType.Int).Value = ValidFromMonth
        com.Parameters.Add("@ValidFromYear", Data.SqlDbType.Int).Value = ValidFromYear
        com.Parameters.Add("@ExpireYear", Data.SqlDbType.Int).Value = ExpireYear
        com.Parameters.Add("@LastBilled", Data.SqlDbType.DateTime).Value = LastBilled
        com.Parameters.Add("@Active", Data.SqlDbType.Bit).Value = Active
        conn.Open()
        reader = com.ExecuteReader()
        reader.Close()

    End Sub

    Sub DeleteCreditCard(Id As Integer)
        conn = New SqlConnection(connectionString)
        com = New SqlCommand()

        com.Connection = conn

        com.CommandType = CommandType.StoredProcedure

        com.CommandText = "pay.usp_CardDetailsDelete"

        com.Parameters.Add("@Id", Data.SqlDbType.Int).Value = Id
        conn.Open()
        reader = com.ExecuteReader()
        reader.Close()

        GridView1.DataBind()
        Response.Redirect("~/private/secure_members/manage/MyCardDetails.aspx")


    End Sub


    Sub FillYearsValidy()
        Dim year = CInt(Date.Now.Year) - 2004
        Dim yearsDT As DataTable = GetYearsTable()
        For index = 0 To 4
            yearsDT.Rows.Add(year + index)
        Next
        cboCardValidEditYY.DataSource = yearsDT
        cboCardValidEditYY.DataTextField = "Year"
        cboCardValidEditYY.DataValueField = "Year"
        cboCardValidEditYY.DataBind()
    End Sub


    Sub FillYearsExpiry()
        Dim year = CInt(Date.Now.Year) - 2000
        Dim yearsDT As DataTable = GetYearsTable()
        For index = 0 To 5
            yearsDT.Rows.Add(year + index)
        Next
        cboCardExpiryEditYY.DataSource = yearsDT
        cboCardExpiryEditYY.DataTextField = "Year"
        cboCardExpiryEditYY.DataValueField = "Year"
        cboCardExpiryEditYY.DataBind()
    End Sub

    Function GetYearsTable() As DataTable
        ' Create new DataTable instance.
        Dim table As New DataTable
        ' Create four typed columns in the DataTable.
        table.Columns.Add("Year", GetType(Integer))

        Return table
    End Function
    Function CheckEditCardNumber() As Boolean

        ' Validation not working
        'Return True

        Dim validation As Boolean
        Dim cardnum = txtEditCardNumber.Text.Replace("""", "").Replace(" ", "")
        'txtCardType1.Text = cardnum
        'cboCCType
        Dim selected = cboCCEditType.SelectedValue.ToString()
        If cardnum.Length > 0 Then
            If cardnum.Length > 4 Then
                Dim trimedtext As String = cardnum
                Dim trimedtext1 As String = trimedtext.Substring(0, 1)
                Dim trimedtext2 As String = trimedtext.Substring(0, 2)
                Dim trimedtext3 As String = trimedtext.Substring(0, 3)
                Dim trimedtext4 As String = trimedtext.Substring(0, 4)
                If selected = "1" Then
                    If CDbl(trimedtext1) = 4 Then
                        If trimedtext.Length = 13 OrElse trimedtext.Length = 16 Then
                            MsgEditLbl.Text += ("Visa is ok")

                            validation = True
                        Else
                            MsgEditLbl.Text += ("Visa is NOT ok")
                            validation = False
                        End If
                    Else
                        MsgEditLbl.Text += ("Visa number is NOT ok")
                    End If


                End If
                If selected = "2" Then
                    If CDbl(trimedtext2) = 36 OrElse CDbl(trimedtext2) = 51 OrElse CDbl(trimedtext2) = 52 OrElse CDbl(trimedtext2) = 53 OrElse CDbl(trimedtext2) = 54 OrElse CDbl(trimedtext2) = 55 Then
                        If trimedtext.Length = 14 OrElse trimedtext.Length = 16 Then
                            MsgEditLbl.Text = ("Mastercard is ok")
                            validation = True
                        Else
                            MsgEditLbl.Text = ("Mastercard is NOT ok")
                            validation = False
                        End If
                    Else
                        MsgEditLbl.Text = ("Mastercard number is NOT ok")
                    End If

                End If

            End If
        Else
            MsgEditLbl.Text = "Please enter valid cart number it must be more then 4 digits."
            validation = False
        End If
        Return validation
    End Function
    Function CheckEditCVVNumber() As Boolean
        Dim validation As Boolean
        Dim number As Integer
        Dim CVVnum As Boolean = Int32.TryParse(txtEditCVV.Text, number)
        If CVVnum Then
            If txtEditCVV.Text.Length = 4 Then

                If CVVnum Then
                    MsgEditLbl.Text += ("CVV has three numbers")
                    validation = True
                End If
            Else
                MsgEditLbl.Text += ("CVV must not be longer then three numbers")
                validation = False
            End If
        Else
            MsgEditLbl.Text += ("CVV has to be number")
            validation = False
        End If
        Return validation
    End Function
    Function CheckEditExpDate() As Boolean
        Dim validation As Boolean
        If cboCardExpiryEditMM.SelectedItem IsNot Nothing AndAlso cboCardExpiryEditYY.SelectedItem IsNot Nothing Then
            Dim selectedmonth = cboCardExpiryEditMM.SelectedItem.ToString()
            Dim selectedyear = cboCardExpiryEditYY.SelectedItem.ToString()

            Dim currentmonth = Date.Today.Month
            Dim currentyear = Date.Today.Year

            If (currentyear - 2000).ToString() = selectedyear Then
                If currentmonth > CInt(selectedmonth) Then
                    MsgEditLbl.Text += ("Cart expired")
                    validation = False
                Else
                    validation = True
                End If
            ElseIf Not (currentyear - 2000).ToString() > selectedyear Then
                validation = True
            Else
                MsgEditLbl.Text += ("Cart Edit expired")
                validation = False
            End If

        Else

        End If

        Return validation

    End Function

    'Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
    '    If e.CommandName = "CustomDelete" Then

    '        carddetailsDT = carddetailsTA.GetData(Nothing)

    '        Dim cards = (From q In carddetailsDT.AsEnumerable() Where q.Id = Convert.ToInt32(e.CommandArgument) Select q).FirstOrDefault()

    '        DeleteCreditCard(cards.Id)

    '    ElseIf e.CommandName = "CustomEdit" Then

    '        carddetailsDT = carddetailsTA.GetData(Nothing)

    '        Dim cards = (From q In carddetailsDT.AsEnumerable() Where q.Id = Convert.ToInt32(e.CommandArgument) Select q).FirstOrDefault()
    '        txtEditFirstName.Text = cards.FirstName
    '        txtEditLastName.Text = cards.LastName
    '        txtEditCardNumber.Text = cards.CardNumber
    '        txtEditL4D.Text = cards.L4D
    '        txtEditCVV.Text = "0116"
    '        lblId.Text = CStr(cards.Id)
    '        cboCCEditType.SelectedValue = cards.CardTypeId
    '        cboCardValidEditMM.SelectedValue = cards.ValidFromMonth
    '        cboCardValidEditYY.SelectedValue = cards.ValidFromYear
    '        cboCardExpiryEditMM.SelectedValue = cards.ExpireMonth
    '        cboCardExpiryEditYY.SelectedValue = cards.ExpireYear
    '        'EditCardModalPopupExtender.Show()
    '    End If

    'End Sub


    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        'If CheckEditCardNumber() AndAlso CheckEditExpDate() AndAlso CheckEditCVVNumber() Then
        If Not GridView1.SelectedValue Is Nothing Then

            If CheckEditCardNumber() AndAlso CheckEditExpDate() Then
                carddetailsDT = carddetailsTA.GetData(CInt(lblId.Text))
                For Each cRow As LogginsLogDS.usp_CardDetailsSelectRow In carddetailsDT
                    UpdateCreditCard(CInt(lblId.Text), _
                                      CInt(Session("SelectedEntityID")), _
                                      cRow.FirstName, _
                                      cRow.LastName, _
                                      txtEditCardNumber.Text.Replace("""", "").Replace(" ", ""), _
                                      txtEditCVV.Text, _
                                      cRow.L4D, _
                                      CInt(cboCCEditType.SelectedValue), _
                                      CInt(cboCardExpiryEditMM.SelectedValue), _
                                      CInt(cboCardExpiryEditYY.SelectedValue), _
                                      CInt(cboCardValidEditMM.SelectedValue), _
                                      CInt(cboCardValidEditYY.SelectedValue), _
                                      cRow.LastBilled, _
                                      True)
                    MsgEditLbl.Visible = False
                    GridView1.DataBind()
                    'Response.Redirect("~/private/secure_members/payment/ConfirmOrder.aspx")
                    Response.Redirect("~/private/secure_members/manage/MyCardDetails.aspx")

                Next


            Else


                MsgEditLbl.Visible = True
            End If
        Else
            MsgBox("Select Credit Card first")
        End If

    End Sub

    'Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    '    Response.Redirect("~/private/secure_members/payment/ConfirmOrder.aspx")
    'End Sub

    Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Response.Redirect("~/private/default.aspx")
    End Sub

    Protected Sub ObjectDataSource3_Selecting(sender As Object, e As ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource3.Selecting
        e.InputParameters("Id") = Nothing

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
        carddetailsDT = carddetailsTA.GetData(Nothing)

        Dim cards = (From q In carddetailsDT.AsEnumerable() Where q.Id = Convert.ToInt32(GridView1.SelectedValue) Select q).FirstOrDefault()
        'txtEditFirstName.Text = cards.FirstName
        'txtEditLastName.Text = cards.LastName
        txtEditCardNumber.Text = cards.CardNumber
        'txtEditL4D.Text = cards.L4D
        txtEditCVV.Text = "0116"
        lblId.Text = CStr(cards.Id)
        cboCCEditType.SelectedValue = cards.CardTypeId
        cboCardValidEditMM.SelectedValue = cards.ValidFromMonth
        cboCardValidEditYY.SelectedValue = cards.ValidFromYear
        cboCardExpiryEditMM.SelectedValue = cards.ExpireMonth
        cboCardExpiryEditYY.SelectedValue = cards.ExpireYear
        'pnlEditCard.Visible = True
        'lblEditDeleteTitle.Text = "Edit Card"

        TabContainer1.ActiveTabIndex = 0

    End Sub

    Protected Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If Not GridView1.SelectedValue Is Nothing Then



            carddetailsDT = carddetailsTA.GetData(Nothing)

            Dim cards = (From q In carddetailsDT.AsEnumerable() Where q.Id = Convert.ToInt32(GridView1.SelectedValue) Select q).FirstOrDefault()

            DeleteCreditCard(cards.Id)
            'pnlEditCard.Visible = False
            'lblEditDeleteTitle.Text = "Select Credit Card for edit"

            Response.Redirect("~/private/secure_members/manage/MyCardDetails.aspx")
        Else
            MsgBox("Select Credit Card first")
        End If
    End Sub
End Class
